<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/Blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/Blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/Blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/Blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/Blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/Blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/Blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="JavaScript,ES6," />










<meta name="description" content="简明 ES6 模块1.什么是模块模块就是一段代码，这段代码可以反复使用，经常单独写成一个文件,一旦加载会立即执行。 2.导出导出有 2 种方式：命名导出和默认导出，分别用关键字export和export default表示 2.1 命名导出:export 关键字 第一种方式:在要导出的代码前加上 export 关键字就可以了!  123456export var foo;export let fo">
<meta property="og:type" content="article">
<meta property="og:title" content="说ES6的模块">
<meta property="og:url" content="https://jingping-ye.github.io/Blog/2019/04/04/%E8%AF%B4ES6%E7%9A%84%E6%A8%A1%E5%9D%97/index.html">
<meta property="og:site_name" content="jingping.ye">
<meta property="og:description" content="简明 ES6 模块1.什么是模块模块就是一段代码，这段代码可以反复使用，经常单独写成一个文件,一旦加载会立即执行。 2.导出导出有 2 种方式：命名导出和默认导出，分别用关键字export和export default表示 2.1 命名导出:export 关键字 第一种方式:在要导出的代码前加上 export 关键字就可以了!  123456export var foo;export let fo">
<meta property="article:published_time" content="2019-04-04T15:30:25.000Z">
<meta property="article:modified_time" content="2020-05-14T05:46:27.420Z">
<meta property="article:author" content="jingping.ye">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="ES6">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/Blog/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://jingping-ye.github.io/Blog/2019/04/04/说ES6的模块/"/>





  <title>说ES6的模块 | jingping.ye</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/Blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">jingping.ye</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">jingping.ye的Blog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/Blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/Blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/Blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/Blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://jingping-ye.github.io/Blog/Blog/2019/04/04/%E8%AF%B4ES6%E7%9A%84%E6%A8%A1%E5%9D%97/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jingping.ye">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/Blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="jingping.ye">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">说ES6的模块</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-04T23:30:25+08:00">
                2019-04-04
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2020-05-14T13:46:27+08:00">
                2020-05-14
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/Blog/categories/%E6%8A%80%E6%9C%AF/" itemprop="url" rel="index">
                    <span itemprop="name">技术</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.3k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="简明-ES6-模块"><a href="#简明-ES6-模块" class="headerlink" title="简明 ES6 模块"></a>简明 ES6 模块</h1><h2 id="1-什么是模块"><a href="#1-什么是模块" class="headerlink" title="1.什么是模块"></a>1.什么是模块</h2><p>模块就是一段代码，这段代码可以反复使用，经常单独写成一个文件,一旦加载会立即执行。</p>
<h2 id="2-导出"><a href="#2-导出" class="headerlink" title="2.导出"></a>2.导出</h2><p>导出有 2 种方式：命名导出和默认导出，分别用关键字<code>export</code>和<code>export default</code>表示</p>
<h3 id="2-1-命名导出-export-关键字"><a href="#2-1-命名导出-export-关键字" class="headerlink" title="2.1 命名导出:export 关键字"></a>2.1 命名导出:export 关键字</h3><ul>
<li>第一种方式:在要导出的代码前加上 export 关键字就可以了!</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">var</span> foo;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">let</span> foo;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> foo;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">myFunc</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span>* <span class="title">myGenFunc</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">MyClass</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>第二种方式:将要导出的对象放在<code>export {}</code>的<code>{}</code>中</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> foo1;</span><br><span class="line"><span class="keyword">let</span> foo2;</span><br><span class="line"><span class="keyword">const</span> foo3;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunc</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyClass</span> </span>&#123;&#125;</span><br><span class="line"><span class="keyword">export</span> &#123;foo1, foo2, foo3, myFunc, MyClass&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-2-默认导出-export-default-关键字"><a href="#2-2-默认导出-export-default-关键字" class="headerlink" title="2.2 默认导出:export default 关键字"></a>2.2 默认导出:export default 关键字</h3><ul>
<li>第一种方式:在要导出的函数或者类之前加 export default 关键字，其中名称可以省略。这将会使其具有匿名函数和匿名类的功能。</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">myFunc</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span>* <span class="title">myGenFunc</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span>* (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">MyClass</span> </span>&#123;&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> </span>&#123;&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> foo;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="string">"Hello world!"</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="number">3</span> * <span class="number">7</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;);</span><br></pre></td></tr></table></figure>

<ul>
<li>第二种方式:用<code>export default moduleName</code>关键字导出</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">=========================</span><br><span class="line"><span class="keyword">var</span> foo1;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> foo1;</span><br><span class="line">==========================</span><br><span class="line"><span class="keyword">let</span> foo2;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">let</span> foo2;</span><br><span class="line">==========================</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">myFunc</span>(<span class="params"></span>) </span>&#123;&#125;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> myFunc;</span><br></pre></td></tr></table></figure>

<p><strong>注意:使用命名导出时，一个 js 文件可以 export 多个模块；但是使用默认导出时，一个文件只能有一个模块导出，否则将会报错。</strong></p>
<h3 id="2-3-其他"><a href="#2-3-其他" class="headerlink" title="2.3 其他"></a>2.3 其他</h3><h4 id="2-3-1-重命名导出"><a href="#2-3-1-重命名导出" class="headerlink" title="2.3.1 重命名导出"></a>2.3.1 重命名导出</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> &#123; MY_CONST <span class="keyword">as</span> FOO, myFunc &#125;;</span><br><span class="line"><span class="keyword">export</span> &#123; foo <span class="keyword">as</span> <span class="keyword">default</span> &#125;;</span><br></pre></td></tr></table></figure>

<h4 id="2-3-2-从其他模块导出"><a href="#2-3-2-从其他模块导出" class="headerlink" title="2.3.2 从其他模块导出"></a>2.3.2 从其他模块导出</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> * <span class="keyword">from</span> <span class="string">"src/other_module"</span>;</span><br><span class="line"><span class="keyword">export</span> &#123; foo <span class="keyword">as</span> myFoo, bar &#125; <span class="keyword">from</span> <span class="string">"src/other_module"</span>;</span><br><span class="line"><span class="keyword">export</span> &#123; <span class="keyword">default</span> &#125; <span class="keyword">from</span> <span class="string">"src/other_module"</span>;</span><br><span class="line"><span class="keyword">export</span> &#123; <span class="keyword">default</span> <span class="keyword">as</span> foo &#125; <span class="keyword">from</span> <span class="string">"src/other_module"</span>;</span><br><span class="line"><span class="keyword">export</span> &#123; foo <span class="keyword">as</span> <span class="keyword">default</span> &#125; <span class="keyword">from</span> <span class="string">"src/other_module"</span>;</span><br></pre></td></tr></table></figure>

<h2 id="3-导入"><a href="#3-导入" class="headerlink" title="3.导入"></a>3.导入</h2><table>
<thead>
<tr>
<th>导入方式</th>
<th>语法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>默认导入</td>
<td><code>import localName from &#39;src/my_lib&#39;;</code></td>
<td>-</td>
</tr>
<tr>
<td>命名空间导入</td>
<td><code>import * as my_lib from &#39;src/my_lib&#39;;</code></td>
<td>-</td>
</tr>
<tr>
<td>命名导入</td>
<td><code>import { name1, name2 } from &#39;src/my_lib&#39;;</code></td>
<td>-</td>
</tr>
<tr>
<td>重命名导入</td>
<td><code>import { name1 as localName1, name2 } from &#39;src/my_lib&#39;;</code></td>
<td>-</td>
</tr>
<tr>
<td>空导入</td>
<td><code>import &#39;src/my_lib&#39;;</code></td>
<td>仅加载模块，不导入任何内容</td>
</tr>
<tr>
<td>默认导入+命名空间</td>
<td><code>import theDefault, * as my_lib from &#39;src/my_lib&#39;;</code></td>
<td>-</td>
</tr>
<tr>
<td>默认导入+命名导入</td>
<td><code>import theDefault, { name1, name2 } from &#39;src/my_lib&#39;;</code></td>
<td>-</td>
</tr>
</tbody></table>
<h2 id="4-导出与导入之家的关联"><a href="#4-导出与导入之家的关联" class="headerlink" title="4.导出与导入之家的关联"></a>4.导出与导入之家的关联</h2><ul>
<li>使用命名导出时，应用<code>import {ModuleName}</code>的方式导入；使用默认导出时，使用<code>import moduleName</code>的方式导入，无需加花括号。</li>
<li>导入的时候，我们可以只导入我们需要的模块，如以下。</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">------lib.js-----</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> PI = <span class="string">'3.14'</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">let</span> perimeter = <span class="function"><span class="params">radius</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'周长:'</span>, <span class="number">2</span> * PI * radius)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">-----main.js-----</span><br><span class="line"><span class="keyword">import</span> &#123;perimeter&#125; <span class="keyword">from</span> <span class="string">'lib'</span></span><br><span class="line">perimeter(<span class="number">1</span>);</span><br></pre></td></tr></table></figure>

<h2 id="5-在-script-中使用模块"><a href="#5-在-script-中使用模块" class="headerlink" title="5. 在 script 中使用模块"></a>5. 在 script 中使用模块</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--加载一个js模块文件--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"module"</span> <span class="attr">src</span>=<span class="string">"module.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 内联引入一个模块 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"module"</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123;sum&#125; <span class="keyword">from</span> <span class="string">'./example.js'</span>;</span></span><br><span class="line"><span class="javascript"><span class="keyword">let</span> res = sum(<span class="number">1</span>,<span class="number">2</span>);</span></span><br></pre></td></tr></table></figure>

<h2 id="6-注意事项"><a href="#6-注意事项" class="headerlink" title="6.注意事项"></a>6.注意事项</h2><ul>
<li>直接使用模块语法在浏览器中可能无效，需要 babel 等工具转为可接受的语法</li>
<li>导出的 2 种方式 export 和 export default 在一个文件中最好用一种，尽量少混用</li>
<li>在 ES6 模块中，default 是一个关键字，不要使用 default 作为 as 的重命名名称</li>
<li>import 后，import 的模块将会被提升，放置于当前代码的最前端。因此，何处导入模块并不重要。</li>
<li>import 应该是单独的一条语句，不能在其他语句中运行该语句。以下 example.js 中的操作将导致错误。</li>
<li>导入的只是当初模块的只读视图，这意味着操作的模块中的变量都存储在模块的内部</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">-----example.js------</span><br><span class="line">if(xxx)&#123;</span><br><span class="line">import example from &quot;mock&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="7-实例"><a href="#7-实例" class="headerlink" title="7.实例"></a>7.实例</h2><p>【实例 1】链接信息</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">-----api.js-----</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">let</span> linkInfo = [</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">'百度'</span>,</span><br><span class="line">    address: <span class="string">'baidu.com'</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">'新浪'</span>,</span><br><span class="line">    address: <span class="string">'sina.com'</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">'优酷'</span>,</span><br><span class="line">    address: <span class="string">'youku.com'</span></span><br><span class="line">  &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">-----main.js-----</span><br><span class="line"><span class="keyword">import</span> &#123;linkInfo&#125; <span class="keyword">from</span> <span class="string">"mock"</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'链接信息:'</span>,<span class="built_in">JSON</span>.stringify(linkInfo))</span><br></pre></td></tr></table></figure>

<p>【实例 2】工具类</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  加解密算法</span></span><br><span class="line"><span class="keyword">import</span> CryptoJS <span class="keyword">from</span> <span class="string">"crypto-js"</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPrivate</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> result = <span class="string">"abcdef"</span>;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">    result += i;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">Encrypt</span>(<span class="params">data</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> src = CryptoJS.enc.Utf8.parse(data);</span><br><span class="line">  <span class="keyword">let</span> encrypted = CryptoJS.AES.encrypt(src, CryptoJS.enc.Utf8.parse(getPrivate()), &#123;</span><br><span class="line">    mode: CryptoJS.mode.ECB,</span><br><span class="line">    padding: CryptoJS.pad.Pkcs7,</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> encrypted.ciphertext.toString(CryptoJS.enc.Base64);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">Decrypt</span>(<span class="params">data</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> decrypt = CryptoJS.AES.decrypt(data, CryptoJS.enc.Utf8.parse(getPrivate()), &#123;</span><br><span class="line">    mode: CryptoJS.mode.ECB,</span><br><span class="line">    padding: CryptoJS.pad.Pkcs7,</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">var</span> result = decrypt.toString(CryptoJS.enc.Utf8);</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>【实例 3】接口</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line">-----api.js-----</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  addLink (link) &#123;</span><br><span class="line">    <span class="keyword">let</span> params = &#123;</span><br><span class="line">      name: link.name,</span><br><span class="line">      address: link.address</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> http</span><br><span class="line">      .post(<span class="string">'/link/add'</span>, params)</span><br><span class="line">      .then(<span class="function"><span class="params">data</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(data)</span><br><span class="line">      &#125;)</span><br><span class="line">      .catch(<span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(e)</span><br><span class="line">      &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  updateLink (link) &#123;</span><br><span class="line">    <span class="keyword">let</span> params = &#123;</span><br><span class="line">      id: link.id,</span><br><span class="line">      name: link.name,</span><br><span class="line">      address: link.address</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> http</span><br><span class="line">      .post(<span class="string">'/link/update'</span>, params)</span><br><span class="line">      .then(<span class="function"><span class="params">data</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(data)</span><br><span class="line">      &#125;)</span><br><span class="line">      .catch(<span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(e)</span><br><span class="line">      &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  getLinkList () &#123;</span><br><span class="line">    <span class="keyword">let</span> params = &#123;</span><br><span class="line">      currentPage: <span class="number">1</span>,</span><br><span class="line">      pageSize: <span class="number">20</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> http</span><br><span class="line">      .post(<span class="string">'/link/list'</span>, params)</span><br><span class="line">      .then(<span class="function"><span class="params">data</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(data)</span><br><span class="line">      &#125;)</span><br><span class="line">      .catch(<span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(e)</span><br><span class="line">      &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  deleteLink (link) &#123;</span><br><span class="line">    <span class="keyword">let</span> params = &#123;</span><br><span class="line">      id: link.id</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> http</span><br><span class="line">      .post(<span class="string">'/link/delete'</span>, params)</span><br><span class="line">      .then(<span class="function"><span class="params">data</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(data)</span><br><span class="line">      &#125;)</span><br><span class="line">      .catch(<span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(e)</span><br><span class="line">      &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">-----main.js-----</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> api <span class="keyword">from</span> <span class="string">"./myLib"</span>;</span><br><span class="line">api.addLink(myParams)</span><br><span class="line">   .then(<span class="function"><span class="params">res</span>=&gt;</span>&#123;<span class="built_in">console</span>.log(<span class="string">"well"</span>)&#125;)</span><br><span class="line">   .catch(<span class="function"><span class="params">e</span> =&gt;</span> &#123;<span class="built_in">console</span>.log(e)&#125;);</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/Blog/tags/JavaScript/" rel="tag"># JavaScript</a>
          
            <a href="/Blog/tags/ES6/" rel="tag"># ES6</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/Blog/2019/04/03/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%9A%84%E6%80%9D%E8%B7%AF%E4%B8%8E%E6%96%B9%E6%B3%95/" rel="next" title="快速排序的思路与方法">
                <i class="fa fa-chevron-left"></i> 快速排序的思路与方法
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/Blog/2019/05/10/%E5%BC%95%E7%94%A8%E8%B1%86%E7%93%A3%E5%9B%BE%E7%89%87%E5%9C%A8%E6%89%8B%E6%9C%BA%E4%B8%8A%E5%A4%B1%E6%95%88/" rel="prev" title="引用豆瓣图片在手机上失效">
                引用豆瓣图片在手机上失效 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">jingping.ye</p>
              <p class="site-description motion-element" itemprop="description">jingping.ye的Blog|文章</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/Blog/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/Blog/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/Blog/tags/index.html">
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yourname" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:yourname@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/yourname" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yourname" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.facebook.com/yourname" target="_blank" title="FB Page">
                      
                        <i class="fa fa-fw fa-facebook"></i>FB Page</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://vk.com/yourname" target="_blank" title="VK Group">
                      
                        <i class="fa fa-fw fa-vk"></i>VK Group</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://stackoverflow.com/yourname" target="_blank" title="StackOverflow">
                      
                        <i class="fa fa-fw fa-stack-overflow"></i>StackOverflow</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://youtube.com/yourname" target="_blank" title="YouTube">
                      
                        <i class="fa fa-fw fa-youtube"></i>YouTube</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://instagram.com/yourname" target="_blank" title="Instagram">
                      
                        <i class="fa fa-fw fa-instagram"></i>Instagram</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="skype:yourname?call|chat" target="_blank" title="Skype">
                      
                        <i class="fa fa-fw fa-skype"></i>Skype</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#简明-ES6-模块"><span class="nav-number">1.</span> <span class="nav-text">简明 ES6 模块</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-什么是模块"><span class="nav-number">1.1.</span> <span class="nav-text">1.什么是模块</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-导出"><span class="nav-number">1.2.</span> <span class="nav-text">2.导出</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-命名导出-export-关键字"><span class="nav-number">1.2.1.</span> <span class="nav-text">2.1 命名导出:export 关键字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-默认导出-export-default-关键字"><span class="nav-number">1.2.2.</span> <span class="nav-text">2.2 默认导出:export default 关键字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-其他"><span class="nav-number">1.2.3.</span> <span class="nav-text">2.3 其他</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1-重命名导出"><span class="nav-number">1.2.3.1.</span> <span class="nav-text">2.3.1 重命名导出</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-2-从其他模块导出"><span class="nav-number">1.2.3.2.</span> <span class="nav-text">2.3.2 从其他模块导出</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-导入"><span class="nav-number">1.3.</span> <span class="nav-text">3.导入</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-导出与导入之家的关联"><span class="nav-number">1.4.</span> <span class="nav-text">4.导出与导入之家的关联</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-在-script-中使用模块"><span class="nav-number">1.5.</span> <span class="nav-text">5. 在 script 中使用模块</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-注意事项"><span class="nav-number">1.6.</span> <span class="nav-text">6.注意事项</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-实例"><span class="nav-number">1.7.</span> <span class="nav-text">7.实例</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">jingping.ye</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">15k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/Blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/Blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/Blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/Blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/Blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/Blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
